﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.jwxcd.CommunityDriver;
using System.Windows.Controls;
using System.Windows;
using PM.Controls.Common;
using System.Threading;

namespace HealthManagement.View
{
    public class MeasureBase : Page
    {
        IytongDriver driver;

        public MeasureBase()
        {
            driver = new IytongDriver();
            driver.onConnectedEvent += onDriverConnected;
            driver.onDisconnectedEvent += onDriverDisConnected;
            driver.onMeausreEvent += onMeasure;
            driver.onNoticeEvent += onNoticeEvent;

            this.Loaded += new System.Windows.RoutedEventHandler(MeasureBase_Loaded);
            this.Unloaded += new System.Windows.RoutedEventHandler(MeasureBase_Unloaded);


            this.Opacity = 0.5;
            this.IsEnabled = false;
        }     


        void MeasureBase_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            driver.connect();
        }

        void MeasureBase_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            driver.disConnect();
        }


        public void onDriverConnected(Object sender, EventArgs e)
        {
            Dispatcher.Invoke(new Action(
                () =>
                {
                    this.Opacity = 1;
                    this.IsEnabled = true;
                    MessageBox.Show(Application.Current.MainWindow, "连接成功");
                }));
        }

        private static object lockObj = new object();
        public void onDriverDisConnected(Object sender, EventArgs e)
        {
            
                //if (Application.Current != null)
                //{//退出mianwindow
                //    Dispatcher.Invoke(
                //        new Action(() => MessageBox.Show(Application.Current.MainWindow, "断开成功")), null);
                //}
                //else
                //{
                //    Dispatcher.Invoke(
                //       new Action(() => MessageBox.Show("断开成功")));
                //}

                //Dispatcher.Invoke(new Action(() => MessageBox.Show("断开成功")));
        }

        public virtual void onMeasure(Object sender, MeasureEventArgs e)
        {
        }

        public virtual void onError(Object sender, MeasureErrorArgs e)
        {
            //Action a = new Action(() => Tools.ShowMessage(false, e.value));
            //Dispatcher.Invoke(a);
           
        }

        void onNoticeEvent(object sender, MeasureNoticeArgs e)
        {
            Dispatcher.Invoke(
                new Action(() => MessageBox.Show(Application.Current.MainWindow, e.value)), null);
           
        }

    }
}
